package net.alche.fa.core.doing.invoker;

import net.alche.fa.core.doing.FaAnnoDoing;
import net.alche.fa.core.model.FaValue;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.alche.fa.core.FaException;
import net.alche.al.util.AC;

// FaMapInvoker
@FaAnnoDoing
@SuppressWarnings("all")
public class FaMapInvoker extends AbstractObjectInvoker {

  // 是否为此对象
  public boolean matched(FaValue objVal, Object obj, String methodName, List<FaValue> params) {
    if (obj instanceof Map) {
      return true;
    }
    return false;
  }

  // 调用
  public FaValue invoke(FaValue objVal, Object obj, String methodName, List<FaValue> params) throws FaException {
    Map map = (Map) obj;
    FaValue first = null;
    if (params != null && !params.isEmpty()) {
      first = params.get(0);
    }
    switch (methodName) {
      case "isEmpty": {
        return new FaValue(map.isEmpty());
      }
      case "get": {
        return new FaValue(map.get(first.getValue()));
      }
      case "put": {
        map.put(first.getValue(), params.get(1).getValue());
        return new FaValue();
      }
      case "size": {
        return new FaValue(map.size());
      }
      case "remove": {
        return new FaValue(map.remove(first.getValue()));
      }
      case "entrySet": {
        return new FaValue(map.entrySet());
      }
      default: {
        throw new FaException(FaException.LESS_METHOD, "fa-map method not find | method = " + methodName,
            "params=" + AC.json.toStr(params));
      }
    }
  }
}